/*	JS
*	Copyright 2011 www.thereformation.co.nz.
*	Version 1
*	This file contains methods for css sprites admin interface
*/

jQuery(document).ready(function($) {

    $(".remove").live('click', function() {
         $(this).parents('.sprite-table').remove();
         return false;
    }); 
    	
	setUpSliders();	
	setUpColourPickers();
	 
    $("select").live('change', function() {
     	var selText = $("option:selected",this).val();
     	var parentEl = $(this).parents('.sprite-table');
     	
     	switch(selText) {
     		case 'IMG_FILTER_GRAYSCALE' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_SELECTIVE_BLUR' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_GRAYSCALE_RED' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_GRAYSCALE_GREEN' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_GRAYSCALE_BLUE' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_GRAYSCALE_YELLOW' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_GREYSCALE_NEGATE' :			             			
     			$(".img-adjust", parentEl).hide();
     			break;				             						             						             						             					             								             		case 'IMG_FILTER_GREYSCALE_COLORIZE' :			             			
     			$(".img-adjust", parentEl).hide();
     			$(".grey-colour", parentEl).show();
     			break;	             
     		case 'IMG_FILTER_NEGATE' :
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_FLEA' :
     			$(".img-adjust", parentEl).hide();
     			break;			             			
     		case 'IMG_FILTER_COLORIZE' :
     			$(".img-adjust", parentEl).hide();
     			$(".colourise", parentEl).show();
     			break;
     		case 'IMG_FILTER_SEPIA' :
     			$(".img-adjust", parentEl).hide();
     			break;
     		case 'IMG_FILTER_CONTRAST' :
     			$(".img-adjust", parentEl).hide();
     			$(".contrast", parentEl).show();
     			break;
     		case 'IMG_FILTER_PIXELATE' :
     			$(".img-adjust", parentEl).hide();
     			$(".pixel", parentEl).show();
     			break;	
     		case 'IMG_FILTER_BRIGHTNESS' :
     			$(".img-adjust", parentEl).hide();
     			$(".brightness", parentEl).show();
     			break;			             			
     		case 'IMG_FILTER_SCATTER' :
     			$(".img-adjust", parentEl).hide();
     			$(".scatter", parentEl).show();
     			break;			             					             		
     	}

    }).trigger('change'); 

	
});

//method sets up sliders, lives in global scope
function setUpSliders() {
    	jQuery(function($) {
    	$( ".slider" ).each(function() {
			var value = parseInt( $( this ).text(), 10 );
			if(!isNaN(value)) {
				var target = $(this).next(":hidden");
				var outPut = $(this).nextAll('label');
				var initText = outPut.text();
					$( this ).empty().slider({
						value:value,
						min: parseInt($( this ).attr('data-min')),
						max: parseInt($( this ).attr('data-max')),
						slide: function( event, ui ) {
							$( target ).val( ui.value );
							$( outPut ).text(initText+ui.value );
						}
					});
				$(outPut).text(initText+value );
			}
		});
	});
}

//method sets up colour pickers, lives in global scope
function setUpColourPickers() {
	jQuery(function($) {
		if ($('.colorpicker').length ) {
			$( ".colorpicker" ).each(function() {
				$(this).hide();
				$(this).farbtastic($(this).next('input'));
				if(!$('.color-picker-title', this).length ) {
					$(this).prepend('<div class="color-picker-title"><strong>Choose a colour</strong></div>');
				}
			});
			
	        $(".colour-input").live('click', function() {
	        	$('.colorpicker').hide();
				$(this).prev('.colorpicker').fadeIn();
	            return false;
	        }); 								
		}
		
		
		$(document).mousedown(function() {
			if ($('.colorpicker').length ) {
				$('.colorpicker').each(function() {
					var display = $(this).css('display');
					if ( display == 'block' )
						$(this).fadeOut();
				});
			}
		});
	});	
}


